#include "head.h"

void login(int socket_fd){
    //验证登录
    int count_error = 0;
    while(1){
        //存储用户名和密码
        char uname[20] = {0};
        char password[20] = {0};
        //用户在客户端输入用户名和密码
        printf("用户名:");
        scanf("%s", uname);
        printf("密码:");
        scanf("%s", password);
        //客户端发送用户名和密码到服务端
        send(socket_fd, uname, sizeof(uname), MSG_NOSIGNAL);
        send(socket_fd, password, sizeof(password), MSG_NOSIGNAL);

        //接收服务端发回的标记判断能否登录
        int flag_login;
        recv(socket_fd, &flag_login, sizeof(int), MSG_WAITALL);
        if(flag_login == 0){
            printf("登录成功！\n");
            break;
        }
        else if(flag_login == 1){
            printf("没有该用户！\n");
        }
        else if(flag_login == 2){
            printf("密码错误！\n");
            count_error += 1;
        }
        if(count_error == 3){
            printf("连续三次输入密码错误！拒绝访问！\n");
            close(socket_fd);
            exit(1);
        }
    }
}
